package org.sosie2.client.authentication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.soap.SOAPMessage;

import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.client.AxisClient;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.configuration.NullProvider;
import org.apache.axis.message.SOAPBody;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.utils.XMLUtils;
import org.apache.log4j.Logger;
import org.apache.ws.security.message.WSSecHeader;
import org.apache.ws.security.message.WSSecUsernameToken;
import org.sosie2.service.framework.Utils;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class AuthenticationClient
{
	public  static final String endPoint = "http://localhost:8080/axis/services/SOSIESimpleAuthenticationWS";
	public  static final String request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
	"<m:serve \r\n" + 
	"xmlns:m=\"http://smth.axis.examples.mule.org\">\r\n" + 
	"lemon" + 
	"</m:serve>";
	private static String username = "gabi";
	private static String password = "geheim";

	static Logger logger = Logger.getLogger(AuthenticationClient.class);
	public static Message finalMsg = null;

	public static void initializeContext()
	{
		MessageContext msgContext;
				
		AxisClient tmpEngine = new AxisClient(new NullProvider());
		msgContext = new MessageContext(tmpEngine);
		InputStream in = new ByteArrayInputStream(Utils.emptySoapMsg.getBytes());
		Message msg = new Message(in);
		
		msg.setMessageContext(msgContext);	 
		
		StringReader strReader = new StringReader(request);		
		try{
		
			logger.info("Getting to the request...");
			Document document = XMLUtils.newDocument(new InputSource(strReader));
			
			// Add request to SOAP body
			SOAPBody body = (SOAPBody) msg.getSOAPBody();					
			body.addDocument(document);
			
			// adding username tokens with WS-sec
			Document doc= msg.getSOAPEnvelope().getAsDocument();
			WSSecHeader securityHeader = new WSSecHeader("",false);
			securityHeader.setMustUnderstand(false);
			securityHeader.insertSecurityHeader(doc);

			WSSecUsernameToken token = new WSSecUsernameToken();
			token.setUserInfo(username,password);
			token.setPasswordType(org.apache.ws.security.WSConstants.PASSWORD_DIGEST);
			doc = token.build(doc, securityHeader);
			
			SOAPMessage SOAPmessage = Utils.toMessage(doc);
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			SOAPmessage.writeTo(baos);
			finalMsg = new Message(baos.toString());
		}
		catch(Exception e)
		{
		logger.warn("Exception : " + e.getMessage());
		}
	}

	public static void main(String args[])
	{
		initializeContext();
			
		try{
			Service  service = new Service();
	        Call call = (Call) service.createCall();
	        call.setTargetEndpointAddress( new java.net.URL(endPoint));
						
			Utils.dumpXML("SOAPMessage:", finalMsg.getSOAPEnvelope().getAsDocument(),logger);
			System.out.println("Sent request and waiting for response...");
			
			//SOAPEnvelope response = (SOAPEnvelope)call.invoke(finalMsg);				
		}
		catch(Exception e)
		{
			logger.warn("Exception occured in Client !");
			e.printStackTrace();
		}
		
	}
	
}

